<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
<!--        <a-button type="primary" icon="plus" @click="handleAdd">树节点</a-button>-->
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowSelection="rowSelection"
        showPagination="auto"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
      </s-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
        @editorContent="handleEditorContent"
      />
      <step-by-step-modal ref="modal" @ok="handleOk"/>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import WangEditor from '../../components/Editor/WangEditor.vue'
import TreeList from '@/views/other/TreeList.vue'
import { getMessageList, createMessage } from '@/api/message'

import StepByStepModal from './modules/StepByStepModal'
import CreateForm from './modules/CreateMessage'
import { create } from 'lodash'

const columns = [
  {
    title: 'ID',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '消息名称',
    dataIndex: 'title'
  },
  {
    title: '消息内容',
    dataIndex: 'content'
  }
]

const statusMap = {
  0: {
    text: '正常'
  },
  1: {
    text: '停用'
  }
}

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    StepByStepModal,
    WangEditor,
    TreeList
  },
  data () {
    this.columns = columns
    return {
      editorContent: '', // 用于存储从子组件获取的富文本值  
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return getMessageList(requestParameters)
          .then(res => {
            const data = res.data
            data.list.forEach(element => {
              element.status = element.status == 0 ? '正常' : '停用'
            })
            return data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      groupList: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    }
  },
  created () {
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    getGroupList () {
      var params = {
        pageNum: 1,
        pageSize: 100
      }
    },
    handleDeleteOk (id) {
      var that = this
      this.$confirm({
        title: '确认要删除吗',
        onOk () {
          deleteAlarm(id).then(res => {
            if (res.code == 200) {
              // 刷新表格
              that.$refs.table.refresh(true)
              that.$message.info('删除成功')
            } else {
              that.$message.info(res.message)
            }
          })
        },
      onCancel () {}
      })
    },
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      console.log('1', record)
      this.mdl = { ...record }
    },
    handleOk () {
      createMessage(this.editorContent).then(res => {
          console.log('res', res)
          this.visible = false
          this.confirmLoading = false
          // 刷新表格
          this.$refs.table.refresh()
          if (res.code == 200) {
            this.$message.info('新增成功')
          } else {
            this.$message.info(res.message)
          }
        })
    },
    handleEditorContent(editorContent) { // 处理从子组件获取的富文本内容  
      this.editorContent = editorContent; // 更新富文本的值   
      console.log(this.editorContent)
      this.visible = false
      this.handleOk()
    },   
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub (record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
